<?php
/**
 * Photo.php
 *
 * 用户相册图片数据映射类
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://momoger.com/license
 *
 * @category	Momoger
 * @package 	Momoger_Model
 * @subpackage 	Mapper
 * @author 		Leo
 * @version    	1.0
 * @copyright  	Copyright © 2009 www.momoger.com All Rights Reserved.
 * @license    	http://momoger.com/license     New BSD License
 */

/** Momoger_Model_Mapper_Abstract */
//require_once 'Abstract.php';

/**
 * 用户相册图片数据映射类
 *
 * @category 	Momoger
 * @package		Momoger_Model
 * @subpackage 	Mapper
 * @copyright  	Copyright © 2009 www.momoger.com All Rights Reserved.
 * @license    	http://momoger.com/license     New BSD License
 */
class Momoger_Model_Mapper_Photo extends Momoger_Model_Mapper_Abstract {

    /**
     * 保存模型数据.
     *
     * @param Momoger_Model_Abstract $model 模型对象
     * @return Momoger_Model_Photo
     */
    public function save(Momoger_Model_Abstract $model) {
    	$key = $model->get('photoID');
        if($key) {
        	return $this->update($model);
        } else {
        	return $this->insert($model);
        }
    }

    /**
     * 保存新模型数据
     *
     * @param Momoger_Model_Abstract $model 模型对象
     * @return Momoger_Model_Photo
     */
    public function insert(Momoger_Model_Abstract $model) {
    	$model->remove('photoID');
    	$data = $model->getAll();
    	$init = array(
	        'userID' => 0,
        	'categoryID' => 1,
        	'photoViews' => 1,
        	'photoCreated' => time(),
        	'photoName' => '',
        	'photoType' => '',
        	'photoSize' => 0,
    		'photoTitle' => '',
    		'photoDesc' => ''
        );
    	$result = $this->getDao()->insert(array_merge($init, $data));
        if ($result) {
        	$model->set(array_merge($init, $data, array('photoID' => $result)));
        }
    	return $model;
    }

    /**
     * 更新模型数据
     *
     * @param Momoger_Model_Abstract $model 模型对象
     * @param string|array $where 更新条件
     * @return Momoger_Model_Photo
     */
    public function update(Momoger_Model_Abstract $model, $where = null) {
    	$data = $model->getAll();
    	$key = (int)$data['photoID'];
    	unset($data['photoID']);
    	if ($where) {
    		$this->getDao()->update($data, $where);
    	} else {
	    	if ($key) {
    			$this->getDao()->update($data, "photoID = '" . $key . "'");
	    	}
    	}
    	return $model;
    }

    /**
     * 删除模型数据
     *
     * @param Momoger_Model_Abstract $model 模型对象
     * @param string|array $where 删除条件
     * @return Momoger_Model_Photo
     */
    public function delete(Momoger_Model_Abstract $model, $where = null) {
    	if ($where) {
    		$this->getDao()->delete($where);
        	$model->reset();
    	} else {
	    	$key = (int)$model->get('photoID');
	        if($key) {
	        	$this->getDao()->delete("photoID = '$key'");
	        	$model->reset();
	        }
    	}
		return $model;
    }

    /**
     * 更加模型身份ID获取模新保存在数据源中的数据, 成功找到更新模型本身数据.
     *
     * @param Momoger_Model_Abstract $model 模型对象
     * @param int|string $key 模型身份ID
     * @return Momoger_Model_Photo
     */
    public function find(Momoger_Model_Abstract $model, $key) {
    	return $this->findRow($model, "photoID = '" . $key . "'");
    }

    /**
     * 获取数据表模型.
     *
     * @return Momoger_Dao_Photo
     */
    protected function getDao() {
        if (null === $this->_dao) {
            $this->setDao('Momoger_Dao_Photo');
        }
        return $this->_dao;
    }
}